/*
 *  cpu_gui/tabdialog.cpp
 *  
 *              CPU Monitor 
 *
 *    Spring Semester.  044800.    Technion.
 *   Copyright (C) 2006  Evgeny Shulman  and Elizabeth Shtrom 
 */


#include <QApplication>
#include <QPushButton>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QFont>
#include <QGridLayout>
#include <QListView>
#include <string>
#include <QRadioButton>
#include <QGroupBox>
#include <QAbstractItemView>


#include "../cpu_consts.h"
#include "../cpu_functions.h"
#include "../cpu_parse_display/cpu_parse.h"
#include "tabdialog.h"
#include "w_im_man.h"
#include "w_pagew.h"
#include "w_cpuid.h"
extern int silent;
extern int fd[8];


 /*************************************** MainWindow ***************************************/

 MyWidget::MyWidget(const int _nrCPUs ,const cpu_set_t _affinityModeDefault, QWidget *parent)
     : QWidget(parent),nrCPUs(_nrCPUs),
     	 isAffinityMode(0),isAffinityModeChanged(0), 
     	affinityModeDefault (_affinityModeDefault)
     	
 {
	char cpuname[7];
	QFont button_fnt ( "Times", 18, QFont::Bold);
	QRadioButton *button;

	tabWidget= new QTabWidget(this);
	//tabWidget->setTabShape(QTabWidget::Triangular);

	Regs =	new RegsTab();
	Gdt =	new GdtTab();
	Ldt =	new LdtTab();
	Tss =	new TssTab();
	Idt =	new IdtTab();
	CpuId = new CpuidTab();
	PgTable = new PageTableTab();
	PgDir = new PageDirTab();
	Idt_monitor =	new ImWindow (_nrCPUs);
	
	tabWidget->addTab(CpuId, "Cpuid");
	tabWidget->addTab(Regs, "Regs");
	tabWidget->addTab(Gdt, "GDT");
	tabWidget->addTab(Ldt, "LDT");
	tabWidget->addTab(Tss, "TSS");
	tabWidget->addTab(Idt, "IDT");
	tabWidget->addTab(PgDir, "PGD");
	tabWidget->addTab(PgTable, "PT");
	tabWidget->addTab(Idt_monitor, "MONITOR");

	ButtonGroup = new QButtonGroup();
	QVBoxLayout *buttonsLayout = new QVBoxLayout;
 	
	for( int i=0; i< _nrCPUs; i++){
        snprintf(cpuname,6, "Cpu %d", i);
 		button = new QRadioButton(cpuname,this);
		if ( !i ) 
			button->setChecked(TRUE);
		if ( _nrCPUs == 1 )
			 button->setEnabled ( FALSE );
		button->setFont(button_fnt);
		buttonsLayout ->addWidget(button);
		ButtonGroup->addButton(button);
		ButtonGroup->setId(button,i); 
        connect(button, SIGNAL( clicked() ), this , SLOT(refresh()) );
     }
	
	
	af_ButtonGroup = new QButtonGroup();
	QVBoxLayout *af_buttonsLayout = new QVBoxLayout;
 	
	 
    button = new QRadioButton("Smp Call",this);
	if ( !isAffinityMode)
		button->setChecked(TRUE);
	button->setFont(button_fnt);
	af_buttonsLayout ->addWidget(button);
	af_ButtonGroup->addButton(button);
	af_ButtonGroup->setId(button,0); 
    connect(button, SIGNAL( clicked() ), this , SLOT(refresh()) );
   
    button = new QRadioButton("Affinity",this);
	if ( isAffinityMode)
		button->setChecked(TRUE);
	button->setFont(button_fnt);
	af_buttonsLayout ->addWidget(button);
	af_ButtonGroup->addButton(button);
	af_ButtonGroup->setId(button,1); 
    connect(button, SIGNAL( clicked() ), this , SLOT(refresh()) );
   
    
	QPushButton *quit = new QPushButton(tr("Quit"),this);
	QPushButton *refresh_btn = new QPushButton(tr("Refresh"),this);
	
	refresh_btn->setFont(button_fnt);
	quit->setFont(button_fnt);


	 buttonsLayout->addStretch(1); 
	 buttonsLayout->addLayout(af_buttonsLayout );
    buttonsLayout->addWidget(build_pid("Affinity" ,&affinity_lb ) );
     buttonsLayout->addWidget(build_pid("MainPid" ,&mainPid_lb ) );
     buttonsLayout->addWidget(build_pid("DataPid", &dataPid_lb ) );
      buttonsLayout->addWidget(refresh_btn);
     buttonsLayout->addWidget(quit);
     
     QHBoxLayout *mainLayout = new QHBoxLayout;
     mainLayout->addLayout(buttonsLayout);
     mainLayout->addWidget(tabWidget);
     setLayout(mainLayout);
     
     setLayout(mainLayout);
	 setWindowTitle(tr("Cpu Monitoring driver for Linux - 32 bit systems"));
	
	
	 connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
	 connect(refresh_btn, SIGNAL(clicked()), this, SLOT(refresh()));
	 connect(PgDir, SIGNAL(ptDoubleClicked(unsigned long)), this, SLOT(set_show_pt(unsigned long)));
	 connect(tabWidget , SIGNAL( currentChanged(int )) ,   this, SLOT(refresh( )));
	 
	connect(Idt_monitor->imView , SIGNAL(doubleClicked( QModelIndex ) ) ,  
		         Idt->tView, SLOT( setCurrentIndex( QModelIndex)));
	connect(Idt_monitor->imView , SIGNAL(doubleClicked( QModelIndex ) ) ,  
		         this, SLOT(show_idt() ) );
		         
	connect(Idt->tView , SIGNAL(doubleClicked( QModelIndex ) ) ,  
	 	       Idt_monitor->imView, SLOT( setCurrentIndex( QModelIndex)));
	connect(Idt->tView , SIGNAL(doubleClicked( QModelIndex ) ) ,  
	 	         this, SLOT(show_idt_monitor() ) );
//	 setGeometry ( 50,50, 900,600);
	 refresh();
 }

QGroupBox*    MyWidget::build_pid (  const QString &str, QLabel **pid_lb_p) {
	QLabel *pid_lb    = new QLabel;
	pid_lb->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	QVBoxLayout *pidLayout = new QVBoxLayout;
	pidLayout->addWidget(pid_lb);
	QGroupBox  *pidGroup   = new QGroupBox(str);
  	 pidGroup->setLayout(pidLayout);

	*pid_lb_p = pid_lb;
	return pidGroup;
}


void MyWidget::show_idt  (void){
	tabWidget->setCurrentWidget(Idt);
}

void MyWidget::show_idt_monitor(void){
	tabWidget->setCurrentWidget(Idt_monitor);
} 


void MyWidget::refresh(){
	MonitorPageTab *curTab = (MonitorPageTab *) tabWidget->currentWidget();
	int cpu = ButtonGroup->checkedId();	
	isAffinityMode = af_ButtonGroup->checkedId();
	if ( isAffinityMode ) {
		isAffinityModeChanged = 1;
		if ( cpu_set_affinity(cpu) )
			return;
	}else if( isAffinityModeChanged  ){
		if ( sched_setaffinity(0, sizeof(affinityModeDefault), &affinityModeDefault) < 0) {
   			fprintf(stderr,"cpu_set_affiniti:sched_setaffinity error.");
   		}
		isAffinityModeChanged =0;
	}
	PDEBUG ("GUI: affinity: mode = %d  changed = %d default =%lx\n", isAffinityMode, isAffinityModeChanged, cpu_get_affinity ( ));
	PDEBUG ("GUI: cpu changed to %d  running update for tab %d\n", cpu, tabWidget->currentIndex() ) ;
	curTab->setUpdateForce();
	curTab->update_curr_cpu(cpu);
	curTab->show_values();
	curTab->getsetPid();
	
	affinity_lb->setText(  QString("%1").arg( cpu_get_affinity(), 8,2)   );
	mainPid_lb->setText(curTab->getMainPid());
	dataPid_lb->setText(curTab->getDataPid());
}
void MyWidget::set_show_pt (unsigned long entry){
	PgTable->set_pt_entry(entry);
	tabWidget->setCurrentWidget(PgTable);
}

 /*************************************** RegsTab ***************************************/

 RegsTab::RegsTab(QWidget *parent)
     : MonitorPageTab(parent)
 {
 	
	
	
	regs_win_db = new RegWin(QString("Debug Regs") ,7);
	regs_win_cr = new RegWin(QString("Control Regs") ,4);
	regs_win_sg = new RegWin(QString("Segment Regs") ,6 );
	regs_win_idtr = new RegWin(QString("IDTR") ,2 );
	regs_win_gdtr = new RegWin(QString("GDTR") ,2 );
	regs_win_tr = new RegWin(QString("*TR") ,2 );
	
	QHBoxLayout *CpuRegsLayout = new QHBoxLayout();
	CpuRegsLayout->addWidget(regs_win_db);
	CpuRegsLayout->addWidget(regs_win_cr);
	CpuRegsLayout->addWidget(regs_win_sg);
	QHBoxLayout *CpuTrLayout    = new QHBoxLayout();
	CpuTrLayout->addWidget(regs_win_idtr);
	CpuTrLayout->addWidget(regs_win_gdtr);
	CpuTrLayout->addWidget(regs_win_tr);
	
	show_values();
	
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(CpuRegsLayout);
	mainLayout->addLayout(CpuTrLayout);
	mainLayout->addStretch(1); 
	setLayout(mainLayout);

 }

int  RegsTab::show_values() {

	int ret = 0;
    int line = 0 ;
    if ( !isUpdate() )
		 return 0 ;
	ret = read_regs (fd[CurrCpu] , &regs );
	if ( ret ) {
		fprintf(stderr, "\nGUI:failed to update regs tab!\n");
		return -1;
	}

	parsed_regs_s p_regs;
	parse_regs(&regs, &p_regs);

	line =0;	
     regs_win_db->set_regs  (line++, "dr0" , p_regs.dr0 );
     regs_win_db->set_regs  (line++, "dr1" , p_regs.dr1 );
     regs_win_db->set_regs  (line++, "dr2" , p_regs.dr2 );
     regs_win_db->set_regs  (line++, "dr3" , p_regs.dr3 );
     regs_win_db->set_regs  (line++, "dr4" , p_regs.dr4 );
     regs_win_db->set_regs  (line++, "dr5" , p_regs.dr5 );
     regs_win_db->set_regs  (line++, "dr6" , p_regs.dr6 );
    
    
   line =0;	
    regs_win_cr->set_regs  (line++, "cr0" , p_regs.cr0 );
    regs_win_cr->set_regs  (line++, "cr2" , p_regs.cr2 );
    regs_win_cr->set_regs  (line++, "cr3" , p_regs.cr3 );
    regs_win_cr->set_regs  (line++, "cr4" , p_regs.cr4 );
    
   line =0; 
    regs_win_sg->set_regs  (line++, "cs" , p_regs.cs  );
    regs_win_sg->set_regs  (line++, "ss" , p_regs.ss  );
    regs_win_sg->set_regs  (line++, "es" , p_regs.es  );
    regs_win_sg->set_regs  (line++, "ds" , p_regs.ds  );
    regs_win_sg->set_regs  (line++, "fs" , p_regs.fs  );
    regs_win_sg->set_regs  (line++, "gs" , p_regs.gs  );

   line =0;
   	regs_win_gdtr->set_regs  (line++, "adress" , p_regs.gdtr.address  );
    regs_win_gdtr->set_regs  (line++, "size"   , p_regs.gdtr.size  );
   line =0;
   	regs_win_idtr->set_regs  (line++, "adress" , p_regs.idtr.address  );
    regs_win_idtr->set_regs  (line++, "size"   , p_regs.idtr.size  );
  
  line =0;
   	regs_win_tr->set_regs  (line++, "ldtr" , p_regs.ldtr);
    regs_win_tr->set_regs  (line++, "tr"   , p_regs.tr  );
    UpdatedCpu = CurrCpu;
    return 0;
}


 /*************************************** GdtTab ***************************************/


 GdtTab::GdtTab(QWidget *parent)
     : MonitorPageTab(parent) 
 {
 	int line = 0;
  
  tView = new QTreeView(this);
  tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
  ListHeader = new QStandardItemModel(GDT_ENTRIES, 11 , this);

  tView->setAlternatingRowColors(true);
 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Entry   ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Content  ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Status    "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"      Base     ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"      Limit    "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"     Type      ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"G");			
	ListHeader->setHeaderData(line++, Qt::Horizontal,"DPL");	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"D/B");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Mode");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Description");

  show_values();
	
	tView->setModel(ListHeader);
	
	for (int i=0 ; i<11 ; i++ )
		tView->resizeColumnToContents(i);

 	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(tView);
	setLayout(mainLayout);

 }

 int GdtTab::show_values(void){

	int ret = 0;
	int j = 0 ;
	char desc[40];

	if ( !isUpdate() )
		 return 0 ;
	
	ret = read_gdt(fd[CurrCpu] , gdt_table );
	
	if ( ret ) return  ret;
	
	parsed_desc_s p_gdt_table[GDT_ENTRIES];
	parse_gdt(gdt_table,p_gdt_table, 0 ,	GDT_ENTRIES);
	
	for (int i = 0; i < GDT_ENTRIES; i++, j=0) {
		snprintf(desc,40,"%s %s", p_gdt_table[i].type_1 ,  p_gdt_table[i].type_2);			
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].index );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].entry );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].status);
	  ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].base  );
	  ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].limit );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].type  ); 
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].g     );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].dpl   );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].db    );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_gdt_table[i].mode  );
		ListHeader->setData (ListHeader->index( i, j++ ) , desc                 ); 
		
	}	
	 UpdatedCpu = CurrCpu;
   
	return 0 ;
}
	

 /*************************************** LdtTab ***************************************/

 LdtTab::LdtTab(QWidget *parent)
     : MonitorPageTab(parent)
 {
 	int line = 0;
	Status = new QLabel( );
	tView = new QTreeView(this);
	ListHeader = new QStandardItemModel(LDT_ENTRIES, 11 , this);
	
	tView->setAlternatingRowColors(true);
	tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Entry   ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Content  ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Status    "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Base   ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Limit  "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"     Type      ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"G");	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"DPL");	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"D/B");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Mode");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Description");
		
	show_values();
	
	tView->setModel(ListHeader);
	
	for (int i=0 ; i<11 ; i++ )
		tView->resizeColumnToContents(i);
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(Status);
	mainLayout->addWidget(tView);
	mainLayout->setAlignment ( Qt::AlignTop );
	setLayout(mainLayout);
	
 }

 int LdtTab::show_values(void){
 	
	int ret = 0;
	int j = 0 ;
	char desc[40];
	if ( !isUpdate() )
		 return 0 ;
	char msg[20];
	
	ret = read_ldt( fd[CurrCpu] , ldt_table );
	if ( ret ) {
		snprintf( msg , 20 , "No LDT");
		Status->setText ( msg) ;
		Status->setVisible ( true );
		tView->setVisible ( false );
		return ret;
	}
			
	parsed_desc_s p_ldt_table[LDT_ENTRIES];
	parse_ldt(ldt_table,p_ldt_table, 0 ,	LDT_ENTRIES);
	
	for (int i = 0; i < LDT_ENTRIES; i++, j=0) {
		snprintf(desc,40,"%s %s", p_ldt_table[i].type_1 ,  p_ldt_table[i].type_2);			
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].index );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].entry );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].status);
	  ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].base  );
	  ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].limit );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].type  );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].g     ); 
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].dpl   );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].db    );
		ListHeader->setData (ListHeader->index( i, j++ ) , p_ldt_table[i].mode  );
		ListHeader->setData (ListHeader->index( i, j++ ) , desc                 ); 
	}
	 	UpdatedCpu = CurrCpu;
    
    Status->setVisible ( false );
		tView->setVisible ( true );
	return 0 ;	
}
	

/*************************************** PageDirTab ***************************************/


 PageDirTab::PageDirTab( QWidget *parent)
     :MonitorPageTab(parent)
 { 
 	int line = 0;
	tView = new QTreeView(this);
	tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
  
	ListHeader = new QStandardItemModel(PGD_TABLE_ENTRIES, 12, this);
	
	tView->setAlternatingRowColors(true);
	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Entry   ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Content  ");	
	ListHeader->setHeaderData(line++, Qt::Horizontal," PTBase "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Status    ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Avail");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"G");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"PSE");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"A");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"PCD");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"PWT");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"U/S");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"R/W");
	
	tView->setModel(ListHeader);
	for (int i=0 ; i<12 ; i++ )
		tView->resizeColumnToContents(i);
  
 	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(tView);
	setLayout(mainLayout);
	
	connect ( tView, SIGNAL(doubleClicked( QModelIndex ) ), this, SLOT(emit_ptDoubleClicked(QModelIndex ) )   );
 }
 
 
 void PageDirTab::emit_ptDoubleClicked(const QModelIndex &index ){
 	int row = index.row();
 	PDEBUG ( " column = %d  row =%d \n " , index.column() , row  ) ;
 	PDEBUG ( "GUI:emit_ptDoubleClicked: entry clicked : %lx\n",pgdir_table[row]); 
	emit ptDoubleClicked( pgdir_table[row]);
}   

 int PageDirTab::show_values(void){

	int ret = 0;
	int j=0;
	if ( !isUpdate() )
		 return 0 ;
	ret=read_pgd ( fd[CurrCpu] , pgdir_table );
	if ( ret ) return ret;

	parsed_page_s p_pgdir_table[PGD_TABLE_ENTRIES];

	parse_pgdir( pgdir_table , p_pgdir_table , 0 , PGD_TABLE_ENTRIES );

	for (int i = 0; i < PGD_TABLE_ENTRIES; i++, j=0) {
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].index   );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].entry   );   
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].frame   );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].status  );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].avail   );		 
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].global  );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].pse     );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].accessed);
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].pcd     );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].pwt     );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].user    );
		 ListHeader->setData (ListHeader->index( i, j++ ) , p_pgdir_table[i].mode    ); 
	}
	 UpdatedCpu = CurrCpu;
     return 0 ;		
 }

 /*************************************** PageTableTab ***************************************/

 PageTableTab::PageTableTab( QWidget *parent)
     : MonitorPageTab(parent), Entry(0) 
{
	int line = 0 ;
	Status = new QLabel( );
	
	tView = new QTreeView(this);
	tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
  
	ListHeader = new QStandardItemModel(PT_TABLE_ENTRIES, 14, this);
	
	tView->setAlternatingRowColors(true);

	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Entry   ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Content  ");				
	ListHeader->setHeaderData(line++, Qt::Horizontal," PBase  ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"    Status    "); 
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Avail");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"G");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"D");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"A");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"PCD");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"PWT");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"U/S");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"R/W");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"SwpType");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"SwpOff");
		
	tView->setModel(ListHeader);
	tView->setVisible ( false );
	
	for (int i=0 ; i<14 ; i++ )
		tView->resizeColumnToContents(i);
	
	QVBoxLayout * mainLayout = new QVBoxLayout; 
	mainLayout->addWidget(Status);
	mainLayout->addWidget(tView);
  mainLayout->setAlignment ( Qt::AlignTop );
  
	setLayout(mainLayout);

 }

  int PageTableTab::show_values(void){
	int ret = 0;
	int j = 0;
	char msg[100];
	*msg = '\0';
	
	if ( !isUpdate() )
		 return 0 ;
	
	if (! Entry ) {
		snprintf( msg , 50 , " Page Table unavailable : descriptor = %lx ", Entry );
		Status->setText ( msg) ;
		tView->setVisible ( false );
		return -1;
	}
		 
	ret = read_ptable ( fd[CurrCpu] , Entry , pt_table );
	
  if ( ret ) {
  	printf( "GUI: Page table read fault err= %d \n", ret);

  	if ( ret == -PDE_POINTS_TO_PAGE ) 
			snprintf( msg , 100 , "The selected PDE points directly to a 4M page , no page table available : descriptor = 0x%08lx", Entry );

  	if ( ret == -ENTRY_NOT_PRESENT ) 
			snprintf( msg , 100 , "The selected PDE is swapped out : descriptor = 0x%08lx", Entry );
		
		Status->setText ( msg) ;
		tView->setVisible ( false );
	} else {

		snprintf( msg , 100 ,  "Page Table for pgd entry 0x%08lx" ,Entry );
		Status->setText ( msg ) ;

		parsed_page_s p_pt_table[PT_TABLE_ENTRIES];
		parse_ptable ( pt_table  , p_pt_table , 0 , PT_TABLE_ENTRIES );

		for ( int i=0 ; i<PT_TABLE_ENTRIES ; i++ , j=0 ){
			
				ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].index      );
				ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].entry      );   
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].frame      );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].status     );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].avail      );		 
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].global     );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].dirty      );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].accessed   );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].pcd        );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].pwt        );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].user       );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].mode       );
		 		ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].swp_type   );
			  ListHeader->setData (ListHeader->index( i, j++ ) , p_pt_table[i].swp_offset ); 
		}
		tView->setVisible ( true );
	}
	 UpdatedCpu = CurrCpu;
   
	return 0 ;	
}
  
void PageTableTab::set_pt_entry( unsigned long entry ){
		Entry=entry;
		UpdatedCpu = -1;
		PDEBUG ("GUI: set_pt_entry: Entry changed to %lx\n", Entry ) ;
		show_values();
}

 void PageTableTab::update_curr_cpu(int cpu_num){
	Entry=0;
	MonitorPageTab::update_curr_cpu(cpu_num);
}

 /*************************************** TssTab ***************************************/

 TssTab::TssTab(QWidget *parent)
     :  MonitorPageTab(parent)
 {

	tView = new QTreeView(this);
	tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
  
	ListHeader = new QStandardItemModel( 26, 2, this);
		
	tView->setAlternatingRowColors(true);
	
	ListHeader->setHeaderData(0, Qt::Horizontal,"          Parameter          ");
	ListHeader->setHeaderData(1, Qt::Horizontal,"Value"); 
	
	tView->setModel(ListHeader);
	
	for (int i=0 ; i<2 ; i++ )
		tView->resizeColumnToContents(i);
		
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(tView);
	setLayout(mainLayout);
	
	show_values();

}

int TssTab::show_values(void){
	int ret = 0;
	if ( !isUpdate() )
		 return 0 ;
	ret=read_tss( fd[CurrCpu] , &tss_table );
	if ( ret ) return ret;
		
	parsed_tss_s p_tss_table;
	parse_tss( &p_tss_table , &tss_table);
	
	int line=0;
	ListHeader->setData (ListHeader->index(line++,0) , "I/O Map Base Address" );
	ListHeader->setData (ListHeader->index(line++,0) , "LDT Segment Selector" );
	ListHeader->setData (ListHeader->index(line++,0) , "gs"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "fs"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "ds"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "ss"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "cs"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "es"									  );
	ListHeader->setData (ListHeader->index(line++,0) , "edi" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "esi" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "ebp" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "esp" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "ebx" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "edx" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "ecx" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "eax" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "eflags"							  );
	ListHeader->setData (ListHeader->index(line++,0) , "eip" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "cr3 (PDBR)" 					);
	ListHeader->setData (ListHeader->index(line++,0) , "ss2" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "esp2" 								);
	ListHeader->setData (ListHeader->index(line++,0) , "ss1"								  );
	ListHeader->setData (ListHeader->index(line++,0) , "esp1" 								);
	ListHeader->setData (ListHeader->index(line++,0) , "ss0" 									);
	ListHeader->setData (ListHeader->index(line++,0) , "esp0" 								);
	ListHeader->setData (ListHeader->index(line++,0) , "Previous Task Link"   );
	
	line=0;
	
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.iobase    );
		ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ldtsegsel );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.gs        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.fs        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ds        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ss        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.cs        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.es        );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.edi       );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.esi		   );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ebp			 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.esp  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ebx  	 	 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.edx  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ecx  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.eax  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.eflags 	 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.eip  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.cr3  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ss2  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.esp2		   );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ss1  		 );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.esp1      );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.ss0       );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.esp0 	   );
	ListHeader->setData (ListHeader->index(line++,1) , p_tss_table.prevtask  );
	UpdatedCpu = CurrCpu;
   
   
	return 0;	
}

  /*************************************** IdtTab ***************************************/
 IdtTab::IdtTab( QWidget *parent)
     :  MonitorPageTab(parent)
 {
  int line = 0 ;
	tView = new QTreeView(this);
	tView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
  tView->setAlternatingRowColors(true);
    
  ListHeader = new QStandardItemModel( IDT_ENTRIES, 9 , this );
        
	

	ListHeader->setHeaderData(line++, Qt::Horizontal,"  Entry   ");
  ListHeader->setHeaderData(line++, Qt::Horizontal,"  Content ");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"     Type      ");		
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Status");		
	ListHeader->setHeaderData(line++, Qt::Horizontal,"DPL");	
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Mode");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"Permissions");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"SegSel");
	ListHeader->setHeaderData(line++, Qt::Horizontal,"SegOff");    
	
	show_values();
	
	tView->setModel(ListHeader);
		
	for (int i=0 ; i<9 ; i++ )
		tView->resizeColumnToContents(i);
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(tView);
	setLayout(mainLayout);

 }
 
int  IdtTab::show_values(){
	
	int ret = 0;
	int j=0;
	char per[40];
	
	if ( !isUpdate() )
		 return 0 ;
		 
	ret=read_idt ( fd[CurrCpu] , idt_table );
		
	if ( ret ) return ret ;
		
	parsed_desc_s p_idt_table[IDT_ENTRIES];	
		
		parse_idt ( idt_table , p_idt_table , 0 , IDT_ENTRIES );
		
		for (int i = 0; i < IDT_ENTRIES; i++, j=0) {
			snprintf(per,40,"%s %s", p_idt_table[i].type_1,  p_idt_table[i].type_2 );
	  	ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].index  );
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].entry  );      	  	
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].type   );      
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].status );      
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].dpl    );      
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].mode   );
			ListHeader->setData (ListHeader->index( i, j++ ) , per                   );    
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].sel    ); 
			ListHeader->setData (ListHeader->index( i, j++ ) , p_idt_table[i].off    );  

	}

	UpdatedCpu = CurrCpu;
	    
	return 0 ;
}

